﻿using MyCompany.MyProject.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MyCompany.MyProject.Repository.Base
{
    public interface IBaseRepository<TEntity> where TEntity : class
    {
        /// <summary>
        /// 数据库实例
        /// </summary>
        ISqlSugarClient Db { get; }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<long> Add(TEntity entity);

        /// <summary>
        /// 添加分表数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<List<long>> AddSplit(TEntity entity);

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="whereExpression">查询条件</param>
        /// <returns></returns>
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>>? whereExpression);

        /// <summary>
        /// 多表连接查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="joinExpression"></param>
        /// <param name="selectExpression"></param>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<List<TResult>> QueryMuch<T, T2, T3, TResult>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, TResult>> selectExpression, Expression<Func<T, T2, T3, bool>> whereExpression) where T : class, new();

        /// <summary>
        /// 分表查询
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="orderField"></param>
        /// <returns></returns>
        Task<List<TEntity>> QuerySplit(Expression<Func<TEntity, bool>> whereExpression, string orderField = "");

        Task<List<TEntity>> QueryWithCache(Expression<Func<TEntity, bool>>? whereExpression = null);




        /// <summary>
        /// 根据id获取一个对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<TEntity> QueryById(object id);
        /// <summary>
        /// 根据id获取一个对象
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="blnUseCache">是否使用缓存</param>
        /// <returns></returns>
        Task<TEntity> QueryById(object id, bool blnUseCache = false);
        Task<List<TEntity>> QueryByIDs(long[] lstIds);

        /// <summary>
        /// 分页查询数据列表
        /// </summary>
        /// <param name="whereExpression">过滤条件</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByFields">排序字段</param>
        /// <returns></returns>
        Task<PageModel<TEntity>> QueryPage(Expression<Func<TEntity,bool>>? whereExpression = null, int pageIndex = 1, int pageSize = 20, string? orderByFields = null);


        /// <summary>
        /// 根据id批量删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<bool> DeleteByIds(object[] ids);

        /// <summary>
        /// 批量添加实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<List<long>> Add(List<TEntity> entity);


        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<bool> Update(TEntity entity);

        Task<bool> Update(List<TEntity> model);

    }
}
